﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps.Platform;
using Microsoft.Phone.Tasks;
using Studentpartners.Sightseeing.ViewModels;

namespace Studentpartners.Sightseeing
{
    public partial class SightPage : PhoneApplicationPage
    {
        private readonly string _backColor;
        private readonly string _frontColor;

        public SightPage()
        {
            InitializeComponent();
            // checking for the theme colors
            var pap = new PhoneApplicationPage();

            _backColor = Color2WebString((Color)pap.Resources["PhoneBackgroundColor"]);
            _frontColor = Color2WebString((Color)pap.Resources["PhoneForegroundColor"]);
        }

        private string Color2WebString(Color col)
        {
            return String.Format("#{0:X02}{1:X02}{2:X02}", col.R, col.G, col.B);
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Get parameter from query string
            int i = Convert.ToInt16(NavigationContext.QueryString["id"]);

            // Get frist element from sights
            var aktsightViewModel = App.ViewModel.Items.Where(Id => Id.Id == i);

            var curModel = aktsightViewModel.First();

            DataContext = curModel;
            webbr.NavigateToString(WrapDescription(curModel.Description));
        }

        private string WrapDescription(string desc)
        {
            return String.Format("<BODY style=\'color:{0};background-color:{1};\'>{2}</BODY>", _frontColor, _backColor,
                                 desc);
        }


        private void Map_Click(object sender, RoutedEventArgs e)
        {
            BingMapsTask task = new BingMapsTask();
            task.Center = new Location() { Latitude = ((SightViewModel)DataContext).Latitude, Longitude = ((SightViewModel)DataContext).Longitude };
            task.ZoomLevel = 20;
            task.Show();
        }

        private void Route_Click(object sender, RoutedEventArgs e)
        {
            BingMapsDirectionsTask task = new BingMapsDirectionsTask();
            task.Start = new LabeledMapLocation();
            task.End = new LabeledMapLocation(((SightViewModel)DataContext).Title, new Location() { Latitude = ((SightViewModel)DataContext).Latitude, Longitude = ((SightViewModel)DataContext).Longitude });
            task.Show();
        }
    }
}